package dk.itu.spvc.tourtracker.gui;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;

import dk.itu.spvc.tourtracker.model.CurrentState;
import dk.itu.spvc.tourtracker.model.Note;
import dk.itu.spvc.tourtracker.model.Point;

public class PinOverlay extends ItemizedOverlay<PinOverlayItem> {

	// PRIVATE FIELDS 

	// Debug
	private static final String TAG = "TourTracker";
	private static final boolean D = true;
	
	private List<PinOverlayItem> overlays = new ArrayList<PinOverlayItem>();

	private Context context;
	private int state;
		
	// CTOR 

	public PinOverlay(Context context, Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));		
		this.context = context;
		populate();
	}

	// OVERRIDES 

	@Override
	protected PinOverlayItem createItem(int i) {
		return overlays.get(i);
	}

	@Override
	public int size() {
		return overlays.size();
	}

	@Override
	protected boolean onTap(int index) {
		
		if (D) Log.d(TAG, "size of overlay " + size() + " tap index " + index);
				
		if (index >= 0 && index < size()) {

			PinOverlayItem item = overlays.get(index);

			if (state == CurrentState.VIEW) 
			{
				Intent pinIntent = new Intent(context, PinActivity.class);
				pinIntent.putExtra("pinNoteText", item.getSnippet());
				((Activity) context).startActivityForResult(pinIntent, Map.REQUEST_PIN);
			}
			else if (state == CurrentState.RECORD ||
					state == CurrentState.PAUSED) 
			{
				Intent pinIntent = new Intent(context, CreatePinActivity.class);
				pinIntent.putExtra("action", CreatePinActivity.ACTION_EDIT);
				pinIntent.putExtra("pointIndex", index);
				pinIntent.putExtra("pinNoteText", item.getSnippet());
				((Activity) context).startActivityForResult(pinIntent, Map.REQUEST_CREATE_PIN);
			}
			return true;
		}
		
		return false;

	}

	// METHODS 

	public void addOverlay(PinOverlayItem overlay) {
		overlays.add(overlay);
		populate();
	}

	public void deleteOverlay(int index) {
		setLastFocusedIndex(-1);
		overlays.remove(index);		
		populate();
	}
	
	public void addPin(Point point, Note note) {

		PinOverlayItem pinItem = new PinOverlayItem(
				new GeoPoint(
						(int) (point.getLatitude() * 1E6), 
						(int) (point.getLongitude() * 1E6)
				), 
				"Note", 
				note.getText(), 
				point.getPointId()
		);
		
		addOverlay(pinItem);
	}

	public void addPins(List<Point> points, List<Note> notes) {
				
		for (int i = 0; i < points.size(); i++) {
			this.addOverlay(
				new PinOverlayItem(
					new GeoPoint(
						(int) (points.get(i).getLatitude() * 1E6), 
						(int) (points.get(i).getLongitude() * 1E6)
					), 
					"Note", 
					notes.get(i).getText(), 
					points.get(i).getPointId()
				)
			);
		}
	}
	
	public void setState(int state){
		this.state = state;
	}
	
	public void clear() {
        overlays.clear();
        populate();
    }

}
